$(document).ready(function () {
	clientDataForms();
});



function clientDataForms() {
	
	$("#birthday").datepicker({
		showButtonPanel: 	true,
		changeMonth: 		true,
		changeYear: 		true,
		dateFormat: 		'dd-mm-yy',
		//dateFormat: 		'@',
		showAnim:			'fade',
		defaultDate:		'07-07-1980'
		//currentText: 'Now'
	});
	
	var dialogOpts = {
		autoOpen: 	false,
		resizable: 	true,
		draggable:	false,
		show:		'fade',
		hide:		'fade',
		height: 	500,
		width: 		450,
		modal: 		true,
		buttons: {
			"criar": 	function() {
				
				// get data to send to the server ... make some validations here to
				var values = {};
				jQuery.each($('#createClient-form').serializeArray(), function(i, field) {
					values[field.name] = field.value;
				});
				
				sendData2Server(values);
				
				$(this).dialog("close");
			},
			"cancelar": function() {
				
				$(this).dialog("close");
			}
		},

		// ** Callback Options **
		close: 			function() {
			//allFields.val( "" ).removeClass( "ui-state-error" );
			//console.log('close');
		},
		beforeClose: 	function() {},
		create: 		function() {
			//console.log('create');
		},
		drag: 			function() {},
		open: 			function() {}
		// ... more callbacks are available, check jqueryui api
	};
	$("#createClient-div").dialog(dialogOpts);

	$("#create-client-button").on('click', function () {
		$("#createClient-div").dialog("open");
	});


	
	$("#dialog").dialog({
		autoOpen:	false,
		show:		'fade',
		hide:		'fade'
	});
	
	//$("#tmpButton").on('click', function () {
		//$("#dialog").dialog("open");
	//});
}






function sendData2Server(values) {
	
	$.ajax({
		//url:		'/eclipseWP/RadicalBooking/pt/School/createEventAction',
		url:		window.ajaxUrl + '/createClientAction',
		//url:		window.ajaxUrl, 
		type:		'POST',		// type of request to make
		async:		true,		// asynchronous/synchronous request
		dataType:	"json",		// type of data returned from server
		
		// data to be sent to the server
		data:					
		{
			
			// getTime returns val in milliseconds ... convert to seconds (unixtimestamp)
			name:				values.name,
			address:			values.address,
			email:				values.email,
			phone:				values.phone,
			birthday:			values.birthday
			//offerName:			$("#offerList option:selected").text(),
			//offerId:			$("#offerList option:selected").val()
		},

		// request was successful ... 'data' is received from server
		success: 	function(data, textStatus, jqXHR) {
			console.log('data received: '+data);
		},

		// something went wrong ...
		error:		function(jqXHR, textStatus, errorThrown) {	
			//console.log('error');
			//console.log(textStatus);
			//console.log(errorThrown);
		},
		
		// code to run regardless of success or failure
		complete: function(xhr, status) {
			
		}
	});
}


